home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Tracker Server Folder / mac_event.c < prev    next >
Text File  |  1993-09-19  |  10KB  |  352 lines

  1. /* mac_event.c */
  2.  
  3. #include "mac_event.h"
  4. #include <Sound.h>
  5.  
  6.  
  7. Boolean                                ReceivedOpenEventFlag = false;
  8. char                                    FakeKeyBuffer[MAXKEYS];
  9. int                                        KeyBufPtr = 0;
  10.  
  11. Boolean                                QuitPending = false;
  12.  
  13. /* parameters controlling the synthesis, with handy default values. */
  14. short                                    AntiAliasing = true;
  15. short                                    StereoOn = false;
  16. unsigned short                SamplingRate = 22254;
  17. short                                    NumRepeats = 1;
  18. short                                    Speed = 50;
  19. short                                    StereoMix = 0;
  20. short                                    Loudness = 48;
  21. short                                    NumBits = 8; /* this is new in version 1.10! */
  22.  
  23. int                                        RecalibratePlayer = false; /* set when settings change */
  24.  
  25.  
  26. FSSpec                                GlobalFileSpec;
  27.  
  28. ProcessSerialNumber        WhoLaunchedUs;
  29.  
  30.  
  31. /* function to check to see that all required parameters have been gotten */
  32. OSErr        MyGotRequiredParams(AppleEvent* theAppleEvent)
  33.     {
  34.         DescType        ReturnedType;
  35.         Size                ActualSize;
  36.         OSErr                Error;
  37.  
  38.         Error = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  39.             &ReturnedType,NULL,0,&ActualSize);
  40.         if (Error == errAEDescNotFound)
  41.             {
  42.                 return noErr;  /* we got all the params, since no more were found */
  43.             }
  44.          else
  45.             {
  46.                 if (Error == noErr)
  47.                     {
  48.                         BUG("\pDidn't get all required parameters from apple event");
  49.                         return errAEEventNotHandled;  /* missed some, so it failed */
  50.                     }
  51.                  else
  52.                     {
  53.                         BUG("\pAEGetAttributePtr failed in MyGotRequiredParams");
  54.                         return Error;  /* AEGetAttributePtr failed, so we return why */
  55.                     }
  56.             }
  57.     }
  58.  
  59.  
  60. pascal OSErr MyHandleODoc(AppleEvent* theAppleEvent, AppleEvent* reply, long handlerRefcon)
  61.     {
  62.         OSErr                Error;
  63.         FSSpec            MyFSS;  /* place to put file info */
  64.         long                Index,ItemsInList;
  65.         AEDescList    DocList;
  66.         Size                ActualSize;
  67.         AEKeyword        Keywd;
  68.         DescType        ReturnedType;
  69.  
  70.         /* get the direct parameter--a descriptor list--and put it into DocList */
  71.         Error = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &DocList);
  72.         if (Error != noErr)
  73.             {
  74.                 BUG("\pCouldn't get file list");
  75.             }
  76.         if (Error != noErr) return Error;
  77.  
  78.         /* now we read all the additional parameters that MIGHT be present */
  79.         /* determining how to do the synthesis.  They won't be present if the special */
  80.         /* interface program was not used to launch us.  In that case the defaults */
  81.         /* will take over. */
  82.  
  83.         Error = AEGetParamPtr(theAppleEvent,keyAntiAliasing,typeShortInteger,
  84.             &ReturnedType,(void*)&AntiAliasing,sizeof(short),&ActualSize);
  85.         if (Error != noErr)
  86.             {
  87.                 BUG("\pBad keyAntiAliasing"); /* for debugging--don't worry about it */
  88.             }
  89.  
  90.         Error = AEGetParamPtr(theAppleEvent,keyStereoOn,typeShortInteger,
  91.             &ReturnedType,(void*)&StereoOn,sizeof(short),&ActualSize);
  92.         if (Error != noErr)
  93.             {
  94.                 BUG("\pBad keyStereoOn");
  95.             }
  96.  
  97.         Error = AEGetParamPtr(theAppleEvent,keySamplingRate,typeShortInteger,
  98.             &ReturnedType,(void*)&SamplingRate,sizeof(short),&ActualSize);
  99.         if (Error != noErr)
  100.             {
  101.                 BUG("\pBad keySamplingRate");
  102.             }
  103.  
  104.         Error = AEGetParamPtr(theAppleEvent,keyNumRepeats,typeShortInteger,
  105.             &ReturnedType,(void*)&NumRepeats,sizeof(short),&ActualSize);
  106.         if (Error != noErr)
  107.             {
  108.                 BUG("\pBad keyNumRepeats");
  109.             }
  110.  
  111.         Error = AEGetParamPtr(theAppleEvent,keySpeed,typeShortInteger,
  112.             &ReturnedType,(void*)&Speed,sizeof(short),&ActualSize);
  113.         if (Error != noErr)
  114.             {
  115.                 BUG("\pBad keySpeed");
  116.             }
  117.  
  118.         Error = AEGetParamPtr(theAppleEvent,keyStereoMix,typeShortInteger,
  119.             &ReturnedType,(void*)&StereoMix,sizeof(short),&ActualSize);
  120.         if (Error != noErr)
  121.             {
  122.                 BUG("\pBad keyStereoMix");
  123.             }
  124.  
  125.         Error = AEGetParamPtr(theAppleEvent,keyLoudness,typeShortInteger,
  126.             &ReturnedType,(void*)&Loudness,sizeof(short),&ActualSize);
  127.         if (Error != noErr)
  128.             {
  129.                 BUG("\pBad keyLoudness");
  130.             }
  131.  
  132.         Error = AEGetParamPtr(theAppleEvent,keyNumBits,typeShortInteger,
  133.             &ReturnedType,(void*)&NumBits,sizeof(short),&ActualSize);
  134.         if (Error != noErr)
  135.             {
  136.                 BUG("\pBad keyNumBits");
  137.             }
  138.  
  139.         Error = AEGetParamPtr(theAppleEvent,keySenderPSN,typeProcessSerialNumber,
  140.             &ReturnedType,(void*)&WhoLaunchedUs,sizeof(ProcessSerialNumber),&ActualSize);
  141.         if (Error != noErr)
  142.             {
  143.                 BUG("\pBad keySenderPSN");
  144.             }
  145.  
  146.         /* check for missing required parameters */
  147.         Error = MyGotRequiredParams(theAppleEvent);
  148.         if (Error != noErr)
  149.             {
  150.                 BUG("\Not all required params handled");
  151.             }
  152.         if (Error != noErr) return Error;
  153.         /* count the number of descriptor records in the list */
  154.         Error = AECountItems(&DocList,&ItemsInList);
  155.         /* now get each descriptor record from the list, coerce the returned data */
  156.         /* to an FSSpec record, and open the associated file */
  157.         for (Index=1; Index <= ItemsInList; Index += 1)
  158.             {
  159.                 Error = AEGetNthPtr(&DocList,Index,typeFSS,&Keywd,&ReturnedType,
  160.                     (void*)&MyFSS,sizeof(FSSpec),&ActualSize);
  161.                 if (Error == noErr)
  162.                     {
  163.                         GlobalFileSpec = MyFSS;
  164.                     }
  165.                  else
  166.                     {
  167.                         BUG("\pCouldn't get FSSpec out of type list in MyHandleODoc");
  168.                     }
  169.             }
  170.         Error = AEDisposeDesc(&DocList);
  171.  
  172.         ReceivedOpenEventFlag = true;
  173.  
  174.         return Error;
  175.     }
  176.  
  177.  
  178. /* this receives a simulated keypress from the other system.  The idea */
  179. /* was that we could simulate the user pressing '>', '<', and others by */
  180. /* sending apple events.  Unfortunately, since things are precomputed as far */
  181. /* ahead as we have memory for, the commands don't show up for many seconds. */
  182. /* If I ever get around to making this interrupt driven, well, the mechanism */
  183. /* is here ready and waiting. */
  184. pascal OSErr MyHandleKey(AppleEvent* theAppleEvent, AppleEvent* reply, long handlerRefcon)
  185.     {
  186.         OSErr                Error;
  187.         short                KeyPressed = 0; /* default key does nothing */
  188.         long                ActualSize;
  189.         DescType        ReturnedType;
  190.  
  191.         Error = AEGetParamPtr(theAppleEvent,keyKeyPressCharacter,typeShortInteger,
  192.             &ReturnedType,(void*)&KeyPressed,sizeof(short),&ActualSize);
  193.  
  194.         Error = AEGetParamPtr(theAppleEvent,keyAntiAliasing,typeShortInteger,
  195.             &ReturnedType,(void*)&AntiAliasing,sizeof(short),&ActualSize);
  196.         if (Error == noErr)
  197.             {
  198.                 RecalibratePlayer = true;
  199.             }
  200.  
  201.         Error = AEGetParamPtr(theAppleEvent,keyStereoOn,typeShortInteger,
  202.             &ReturnedType,(void*)&StereoOn,sizeof(short),&ActualSize);
  203.         if (Error == noErr)
  204.             {
  205.                 RecalibratePlayer = true;
  206.             }
  207.  
  208.         Error = AEGetParamPtr(theAppleEvent,keySamplingRate,typeShortInteger,
  209.             &ReturnedType,(void*)&SamplingRate,sizeof(short),&ActualSize);
  210.         if (Error == noErr)
  211.             {
  212.                 RecalibratePlayer = true;
  213.             }
  214.  
  215.         Error = AEGetParamPtr(theAppleEvent,keyNumRepeats,typeShortInteger,
  216.             &ReturnedType,(void*)&NumRepeats,sizeof(short),&ActualSize);
  217.         if (Error == noErr)
  218.             {
  219.                 RecalibratePlayer = true;
  220.             }
  221.  
  222.         Error = AEGetParamPtr(theAppleEvent,keySpeed,typeShortInteger,
  223.             &ReturnedType,(void*)&Speed,sizeof(short),&ActualSize);
  224.         if (Error == noErr)
  225.             {
  226.                 RecalibratePlayer = true;
  227.             }
  228.  
  229.         Error = AEGetParamPtr(theAppleEvent,keyStereoMix,typeShortInteger,
  230.             &ReturnedType,(void*)&StereoMix,sizeof(short),&ActualSize);
  231.         if (Error == noErr)
  232.             {
  233.                 RecalibratePlayer = true;
  234.             }
  235.  
  236.         Error = AEGetParamPtr(theAppleEvent,keyLoudness,typeShortInteger,
  237.             &ReturnedType,(void*)&Loudness,sizeof(short),&ActualSize);
  238.         if (Error == noErr)
  239.             {
  240.                 RecalibratePlayer = true;
  241.             }
  242.  
  243.         Error = AEGetParamPtr(theAppleEvent,keyNumBits,typeShortInteger,
  244.             &ReturnedType,(void*)&NumBits,sizeof(short),&ActualSize);
  245.         if (Error == noErr)
  246.             {
  247.                 RecalibratePlayer = true;
  248.             }
  249.  
  250.         Error = noErr;
  251.  
  252.         if (Error == noErr)
  253.             {
  254.                 Error = MyGotRequiredParams(theAppleEvent);
  255.                 if (Error != noErr)
  256.                     {
  257.                         return Error;
  258.                     }
  259.                 if ((KeyBufPtr < MAXKEYS - 1) && (KeyPressed != 0))
  260.                     {
  261.                         FakeKeyBuffer[KeyBufPtr] = KeyPressed;
  262.                         KeyBufPtr += 1;
  263.                     }
  264.                 return noErr;
  265.             }
  266.          else
  267.             {
  268.                 return Error;
  269.             }
  270.     }
  271.  
  272.  
  273. /* forced quit event on system shutdown or user stoppage */
  274. pascal OSErr MyHandleQuit(AppleEvent* theAppleEvent, AppleEvent* reply, long handlerRefcon)
  275.     {
  276.         OSErr            Error;
  277.  
  278.         /* check for missing required parameters */
  279.         Error = MyGotRequiredParams(theAppleEvent);
  280.         if (Error != noErr) return Error;
  281.         QuitPending = true;
  282.         discard_buffer();
  283.         return noErr;
  284.     }
  285.  
  286.  
  287. /* this is a simple event loop.  Not having an interface, we don't pay any */
  288. /* attention to anything but apple events.  This also gives time to other */
  289. /* applications.  One improvement: adding checks for keyboard events might */
  290. /* be handy when the program is run without the interface.  They could just */
  291. /* be queued like they are done in MyHandleKey. */
  292. void            WaitForEvent(long SleepTime)
  293.     {
  294.         EventRecord                MyEvent;
  295.         OSErr                            Error;
  296.  
  297.         WaitNextEvent(everyEvent,&MyEvent,SleepTime,NULL);
  298.         switch (MyEvent.what)
  299.             {
  300.                 case kHighLevelEvent:
  301.                     Error = AEProcessAppleEvent(&MyEvent);
  302.                     break;
  303.                 default:
  304.                     break;
  305.             }
  306.     }
  307.  
  308.  
  309. /* whip off a message to the interface application explaining why the */
  310. /* song sounds like crap! */
  311. void                FatalError(short ErrorID)
  312.     {
  313.         short                            Error;
  314.         AppleEvent                Event;
  315.         AEAddressDesc            AddressDescriptor;
  316.         AppleEvent                Reply;
  317.  
  318.         Error = AECreateDesc(typeProcessSerialNumber,(void*)&WhoLaunchedUs,
  319.             sizeof(ProcessSerialNumber),&AddressDescriptor);
  320.         Error = AECreateAppleEvent(ControlEventClass,ErrorEvent,&AddressDescriptor,
  321.             kAutoGenerateReturnID,kAnyTransactionID,&Event);
  322.         Error = AEPutParamPtr(&Event,keyErrorIDNum,typeShortInteger,
  323.             (void*)&ErrorID,sizeof(short));
  324.         Error = AESend(&Event,&Reply,kAENoReply,kAENormalPriority,kNoTimeOut,NULL,NULL);
  325.     }
  326.  
  327.  
  328. int                    RegisterEventHandlers(void)
  329.     {
  330.         OSErr                Error;
  331.  
  332.         /* installing open document handler */
  333.         Error = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,&MyHandleODoc,0,false);
  334.         if (Error != noErr)
  335.             {
  336.                 return false;
  337.             }
  338.  
  339.         Error = AEInstallEventHandler(ControlEventClass,ControlEvent,&MyHandleKey,0,false);
  340.         if (Error != noErr)
  341.             {
  342.                 return false;
  343.             }
  344.  
  345.         Error = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,&MyHandleQuit,0,false);
  346.         if (Error != noErr)
  347.             {
  348.                 return false;
  349.             }
  350.         return true;
  351.     }
  352.